package com.yhd.controller;

import com.yhd.entity.result.ResultVo;
import com.yhd.model.dto.FileDto;
import com.yhd.utils.FileConfig;
import com.yhd.utils.FileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@Api(value = "文件管理")
@Slf4j
@RequestMapping(value = "/file")
@CrossOrigin
public class FileController extends WebBaseController {

    @ApiOperation(value = "上传文件", notes = "MultipartFile传入参数")
    @PostMapping("/add")
    @ResponseBody
    public ResultVo uploadFile(@ApiParam(value = "上传文件",required = true) MultipartFile multipartFile) {
        if (null != multipartFile){
            String result = FileUtils.uploadFile(multipartFile);
            if (null != result && !"".equals(result)){
                FileDto fileDto = new FileDto();
                fileDto.setRoute(FileConfig.OOS_ROUTE);
                fileDto.setUrl(result);
                return ResultVo.success(fileDto);
            }
        }
        return ResultVo.failure("10002","上传失败");
    }



}
